package com.example.riqrtest;

import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

/**
 * This activity is responsible for handling the event where the notification
 * item has been clicked on.
 * 
 * The activity sets up a view, displaying the complete data of the
 * notification, such as a long message.
 * 
 * Current implementation: Just a text view containing the message.
 * 
 * To use this class (Say, from a service that generated the notification):
 * 
 * Create an intent containing this class. 
 * Use the intent object's putExtra method to pack necessary data into the bundle. 
 *   Refer to the key strings of this class for the data it is designed to handle. 
 * Create a PendingEvent, pass the previous intent in.
 * Using a NotificationBuilder, set the notification's intent so that it launches this activity.
 * 
 * Sample Code
 * 
 * Intent notificationIntent = new Intent(context, HandleNotifyAct.class);
 * notificationIntent.putExtra(HandleNotifyAct.INTENT_TITLE_KEY, "Your title");
 * notificationIntent.putExtra(HandleNotifyAct.INTENT_CONTENT_KEY, "Your message"); 
 * notificationIntent.putExtra(HandleNotifyAct.INTENT_ID_KEY, 1010); 
 * PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
 * 
 * NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); 
 * notificationBuilder = notificationBuilder.setContentIntent(contentIntent);
 * 
 * Notification notify = notificationBuilder.build();
 * 
 */

public class HandleNotifyAct extends Activity {

	/**
	 * Notification's title
	 */
	public static final String INTENT_TITLE_KEY = "NOTIFY_TITLE";
	/**
	 * Notification message itself.
	 */
	public static final String INTENT_CONTENT_KEY = "NOTIFY_CONTENT";
	/**
	 * The time when the notification is made
	 */
	public static final String INTENT_TIME_KEY = "NOTIFY_TIME";
	/**
	 * Unique integer identifier to be used by the system's notification
	 * manager.
	 */
	public static final String INTENT_ID_KEY = "NOTIFY_ID";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.handle_notify_act);

		Intent intent = getIntent();
		unloadIntentData(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.handle_notify_act, menu);
		return true;
	}

	/**
	 * For each field this notification view is supposed to display, unload the
	 * data from the calling intent and put them onto the interface.
	 * 
	 * @param intent
	 */
	@SuppressWarnings("unused")
	private void unloadIntentData(Intent intent) {
		if (intent == null) {
			return;
		}
		// In this particular case, we want to retrieve the title, text, the
		// time of the notification and its unique ID.
		CharSequence title = intent.getCharSequenceExtra(INTENT_TITLE_KEY);
		CharSequence text = intent.getCharSequenceExtra(INTENT_CONTENT_KEY);
		long time = intent.getLongExtra(INTENT_TIME_KEY, -1);
		int id = intent.getIntExtra(INTENT_ID_KEY, -1);
		
		//Check if there's any data missing.
		if (title == null || text == null || time == -1 || id == -1) {
			TextView textview = (TextView) findViewById(R.id.notificationMsgView);
			textview.setText("Corrupted data");
			return;
		}
		
		Date notificationTime = new Date(time);

		// In reality, we will only display the message itself.
		TextView textview = (TextView) findViewById(R.id.notificationMsgView);
		textview.setText(text);
	}
}
